package http.util;

import okhttp3.Cookie;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import timer.saveCoursesManager;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import static http.util.ParseUtil.parseObjectMoved;

public class HtmlUtil {

    public static String getViewStateFromResponseHtml(String responseHtml) {
        if (responseHtml != null) {

            Document doc = Jsoup.parse(responseHtml);
            Element viewStateElement = doc.select("input[name=__VIEWSTATE]").first();
            //Element viewStateElement2 = doc.select("input[name=__VIEWSTATE]").get(0);
            //Element viewStateElement3 = doc.select("input[name=__VIEWSTATEGENERATOR]").first();
            //robClass.logger.info("Element:" + viewStateElement);
            //robClass.logger.info("Element2:" + viewStateElement2);
            //robClass.logger.info("Element3:" + viewStateElement3);
            if (viewStateElement != null) {
                return (viewStateElement.val());
            }
        }
        return null;
    }

    public static String okHttp3CookieToString(List<Cookie> cookies) {
        StringBuilder cookieStr = new StringBuilder();
        if (cookies == null || cookies.size() <= 0) {
            return null;
        }
        for (Cookie cookie : cookies) {
            cookieStr.append(cookie.name()).append("=").append(cookie.value() + ";");
        }
        return cookieStr.toString();
    }

    /**
     * 根据网页解析PE课程Id与课程名
     */
    public static void parsePESelectCourseList(String responseHtml, HashMap<String, String> courseList) {
        Document doc = Jsoup.parse(responseHtml);
        Elements options = doc.select("select").select("option[value]");
//        robClass.logger.info(options);
        for (int i = 2; i < options.size() - 1; i++) {
//            robClass.logger.info("options.get("+i+"):" + options.get(i));
            Element elementClass = options.get(i);
            String[] text = elementClass.text().split("∥");
            if (text.length > 1) {
                if (text != null) {
                    if (!StringUtils.isBlank(text[1]) || !StringUtils.isBlank(text[0])) {
                        //要课程代码优先了……
                        courseList.put(text[0], text[1]);
                        saveCoursesManager.getGlobalIdAndCourseNameCache().put(text[0], text[1]);
                    }
                }
            }
        }
    }


    /**
     * 根据网页解析Common课程Id与课程名
     */
    public static void parseCommonSelectCourseList(String responseHtml, HashMap<String, String> courseList, boolean display) {
//        System.out.println(responseHtml);
//        System.out.println("parseCommonSelectCourseList");
        Document doc = Jsoup.parse(responseHtml);
        if (ParseUtil.parseObjectMoved(responseHtml)) {
            return;
        }
        Elements options = doc.select("span[class=formbox]").select("tbody").select("tr");
        for (int i = 1; i < options.size() - 1; i++) {
            Element elementClass = options.get(i);
            Elements tds = elementClass.select("td");
            if (tds.size() > 1) {
                if (!tds.get(2).html().contains("学分")) {
//                    System.out.println("tds.get(0):" + tds.get(0));
                    /**防止已选课程冲突过滤*/
                    if (tds.get(0).html().contains("name")) {
                        String className = tds.get(2).html().split("</a>")[0].split(">")[1];
                        String classId = tds.get(3).html().split("</td>")[0];
                        String time = tds.get(5).html().split("</td>")[0];
                        String postValue = tds.get(0).html().split("name=\"")[1].split("\"")[0];
                        String hiddenCode = tds.get(2).html().split("</a>")[0].split("xkkh=")[1].split("'")[0];
                        if (display) {
                            System.out.println("========================================");
                            System.out.println("课程名字: " + className);
                            System.out.println("课程代码: " + classId);
                            System.out.println("上课时间: " + time);

                            System.out.println("发包数据: " + postValue);
                            System.out.println("隐藏代码: " + tds.get(2).html().split("</a>")[0].split("xkkh=")[1].split("'")[0]);
                        }
                        /**传入隐藏代码 发包数据*/
                        if (!StringUtils.isBlank(hiddenCode) || !StringUtils.isBlank(postValue)) {
                            courseList.put(hiddenCode, postValue);
                            saveCoursesManager.getGlobalIdAndCourseNameCache().put(hiddenCode, postValue);
                        }
                    }
                }
            }
        }
        if (display) {
            /**自动备份课表到数据库*/
            System.out.println("自动备份课表到数据库");
            saveCoursesManager.saveCourseList();
        }
    }


}

